using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Script.Tween;

enum EaseType
{
    In,
    Out,
    InOut,
    Linear
}

public class TweenMove : MonoBehaviour
{
    delegate Vector3 EaseFuncDel(Vector3 start, Vector3 end, float duration, float timer);

    private EaseFuncDel EaseFunc;
    [SerializeField] private EaseType _easeType;
    [SerializeField] private bool pingpong = false;
    private float duration = 0;
    Vector3 startPosition = Vector3.zero;
    Vector3 endPosition = Vector3.zero;
    private bool isAnimating = false;

    private Transform tran;

    private void Start()
    {
        tran = transform;
        Move(gameObject, transform.position, Vector3.zero, 5);
        EnsureEase();
    }


    private float timeCounter = 0;

    private int pingpongFlag = 1;

    // Update is called once per frame
    void Update()
    {
        if (EaseFunc == null)
        {
            return;
        }

        if (isAnimating)
        {
            timeCounter += Time.deltaTime * pingpongFlag;
            if (timeCounter >= duration || timeCounter <= 0)
            {
                if (pingpong)
                {
                    pingpongFlag *= -1;
                }
                else
                {
                    isAnimating = false;
                }
            }

            // tran.position = startPosition + (endPosition - startPosition) * timeCounter / duration;
            tran.position = EaseFunc(startPosition, endPosition, duration, timeCounter);
        }
    }

    void Move(GameObject go, Vector3 begin, Vector3 end, float time, bool pingpong = true)
    {
        this.pingpong = pingpong;
        this.duration = time;
        this.endPosition = end;
        this.startPosition = begin;
        this.isAnimating = true;
    }
    private void EnsureEase()
    {
        switch (_easeType)
        {
            case EaseType.In:
                EaseFunc = Ease.In;
                break;
            case EaseType.Out:
                EaseFunc = Ease.Out;
                break;
            case EaseType.InOut:
                EaseFunc = Ease.InOut;
                break;
            case EaseType.Linear:
                EaseFunc = Ease.Linear;
                break;
        }
    }
}